package uts.sdk.modules.oneoilNewland;
import uts.sdk.modules.oneoilNewland.R;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

import com.newland.aidl.beeper.AidlBeeper;
import com.newland.aidl.bluetoothbase.AidlBluetoothBase;
import com.newland.aidl.cashbox.AidlCashBox;
import com.newland.aidl.deviceInfo.AidlDeviceInfo;
import com.newland.aidl.deviceService.AidlDeviceService;
import com.newland.aidl.face.AidlFace;
import com.newland.aidl.guestDisplay.AidlGuestDisplay;
import com.newland.aidl.iccard.AidlCPUCard;
import com.newland.aidl.iccard.AidlICCard;
import com.newland.aidl.led.AidlLED;
import com.newland.aidl.magcardreader.AidlMagCardReader;
import com.newland.aidl.pboc.AidlPBOC;
import com.newland.aidl.pinpad.AidlPinpad;
import com.newland.aidl.printer.AidlBluetoothPrinter;
import com.newland.aidl.printer.AidlExtPrinter;
import com.newland.aidl.printer.AidlPrinter;
import com.newland.aidl.rfcard.AidlRFCard;
import com.newland.aidl.scanner.AidlScanner;
import com.newland.aidl.serialComm.AidlSerialComm;
import com.newland.aidl.sm.AidlSMModule;
import com.newland.aidl.terminal.AidlTerminalManage;
import com.newland.aidl.voice.AidlVoiceBroadcast;
import com.newland.aidl.wxpayface.AidlWXPayFace;

import static android.content.Context.BIND_AUTO_CREATE;

/**
 * Author by bxy, Date on 2019/4/10 0010.
 */
public class NewlandService {

    private static AidlDeviceService deviceService;
    private Context context;
    public NewlandService(Context context) {
        this.context = context;
    }

    public void connect(){
		if (deviceService == null) {
			Intent intent = new Intent();
			intent.setPackage("com.newland.mtype.service"); // 使用服务apk的时候使用
			intent.setAction("com.newland.DeviceService");

			context.bindService(intent, serviceConnection, BIND_AUTO_CREATE);

		} else {
			
		}
    }

    public void disconnect(){
		try {
			context.unbindService(serviceConnection);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			deviceService = null;
		}       
    }

    private ServiceConnection serviceConnection = new ServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName name) {
            deviceService = null;
        }

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            deviceService = AidlDeviceService.Stub.asInterface(service);
        }
    };

    public static AidlBeeper getBeeper() {
        try {
            return AidlBeeper.Stub.asInterface(deviceService.getBeeper());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static AidlVoiceBroadcast getVoiceBroadcast() {
        try {
            return AidlVoiceBroadcast.Stub.asInterface(deviceService.getVoiceBroadcast());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    public static AidlLED getLed() {
        try {
            return AidlLED.Stub.asInterface(deviceService.getLed());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AidlSerialComm getSerialPort() {
        try {
            return AidlSerialComm.Stub.asInterface(deviceService.getSerialComm());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AidlScanner getScanner() {
        try {
            return AidlScanner.Stub.asInterface(deviceService.getScanner());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AidlMagCardReader getMagCardReader() {
        try {
            return AidlMagCardReader.Stub.asInterface(deviceService.getMagCardReader());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AidlICCard getInsertCardReader() {
        try {
            return AidlICCard.Stub.asInterface(deviceService.getICCard());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AidlCPUCard getCPUCardReader() {
        try {
            return AidlCPUCard.Stub.asInterface(deviceService.getCPUCard());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AidlRFCard getRFCardReader() {
        try {
            return AidlRFCard.Stub.asInterface(deviceService.getRFCard());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AidlPinpad getPinpad() {
        try {
            return AidlPinpad.Stub.asInterface(deviceService.getPinpad());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AidlPrinter getPrinter() {
        try {
            return AidlPrinter.Stub.asInterface(deviceService.getPrinter());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AidlBluetoothPrinter getBluetoothPrinter() {
        try {
            return AidlBluetoothPrinter.Stub.asInterface(deviceService.getBluetoothPrinter());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AidlPBOC getPBOC() {
        try {
            return AidlPBOC.Stub.asInterface(deviceService.getPBOC());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AidlDeviceInfo getDeviceInfo() {
        try {
            return AidlDeviceInfo.Stub.asInterface(deviceService.getDeviceInfo());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AidlTerminalManage getTerminalManage() {
        try {
            return AidlTerminalManage.Stub.asInterface(deviceService.getTerminal());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AidlGuestDisplay getGuestDisplay() {
        try {
            return AidlGuestDisplay.Stub.asInterface(deviceService.getGuestDisplay());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AidlBluetoothBase getBluetoothBase() {
        try {
            return AidlBluetoothBase.Stub.asInterface(deviceService.getBluetoothBase());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static AidlFace getFace() {
        try {
            return AidlFace.Stub.asInterface(deviceService.getFace());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static AidlWXPayFace getwxpayFace() {
        try {
            return AidlWXPayFace.Stub.asInterface(deviceService.getWXPayFace());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static AidlCashBox getCashBox() {
        try {
            return AidlCashBox.Stub.asInterface(deviceService.getCashBox());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static AidlSMModule getSMModule() {
        try {
            return AidlSMModule.Stub.asInterface(deviceService.getSMModule());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static AidlExtPrinter getExtPrinter() {
        try {
            return AidlExtPrinter.Stub.asInterface(deviceService.getExtPrinter());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
